package zarzadzaniepracownikami;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class ViewProfile extends JDialog implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = -755572807799644665L;
	private final JTextField nameField = new JTextField();
	private JTextField surnameField;
	private JTextField possitionField;
	private JTextField timeOfWorkField;
	private JButton btnOkButton;
	private JButton btnEdit;
	private Worker worker;
	private Vector<Worker> workers;
	private DefaultListModel workerList;

	/**
	 * Create the dialog.
	 */
	public ViewProfile(String titile, Worker worker, Vector<Worker> workerVector, DefaultListModel listModel) { 
		
		setTitle(titile);
		setResizable(false);
		getContentPane().setEnabled(false);
		setBounds(100, 100, 312, 125);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		getContentPane().setLayout(gridBagLayout);
		
		JLabel lblName = new JLabel("Imie:");
		GridBagConstraints gbc_lblName = new GridBagConstraints();
		gbc_lblName.anchor = GridBagConstraints.EAST;
		gbc_lblName.insets = new Insets(0, 0, 5, 5);
		gbc_lblName.gridx = 1;
		gbc_lblName.gridy = 1;
		getContentPane().add(lblName, gbc_lblName);
		GridBagConstraints gbc_nameField = new GridBagConstraints();
		gbc_nameField.insets = new Insets(0, 0, 5, 0);
		gbc_nameField.gridx = 2;
		gbc_nameField.gridy = 1;
		nameField.setEditable(false);
		getContentPane().add(nameField, gbc_nameField);
		nameField.setColumns(10);
		
		JLabel lblSurname = new JLabel("Nazwisko:");
		GridBagConstraints gbc_lblSurname = new GridBagConstraints();
		gbc_lblSurname.anchor = GridBagConstraints.EAST;
		gbc_lblSurname.insets = new Insets(0, 0, 5, 5);
		gbc_lblSurname.gridx = 1;
		gbc_lblSurname.gridy = 2;
		getContentPane().add(lblSurname, gbc_lblSurname);
		
		surnameField = new JTextField();
		surnameField.setEditable(false);
		GridBagConstraints gbc_surnameField = new GridBagConstraints();
		gbc_surnameField.insets = new Insets(0, 0, 5, 0);
		gbc_surnameField.fill = GridBagConstraints.HORIZONTAL;
		gbc_surnameField.gridx = 2;
		gbc_surnameField.gridy = 2;
		getContentPane().add(surnameField, gbc_surnameField);
		surnameField.setColumns(10);
		
		JLabel lblPossition = new JLabel("Stanowisko:");
		GridBagConstraints gbc_lblPossition = new GridBagConstraints();
		gbc_lblPossition.anchor = GridBagConstraints.EAST;
		gbc_lblPossition.insets = new Insets(0, 0, 5, 5);
		gbc_lblPossition.gridx = 1;
		gbc_lblPossition.gridy = 3;
		getContentPane().add(lblPossition, gbc_lblPossition);
		
		possitionField = new JTextField();
		possitionField.setEditable(false);
		GridBagConstraints gbc_possitionField = new GridBagConstraints();
		gbc_possitionField.insets = new Insets(0, 0, 5, 0);
		gbc_possitionField.fill = GridBagConstraints.HORIZONTAL;
		gbc_possitionField.gridx = 2;
		gbc_possitionField.gridy = 3;
		getContentPane().add(possitionField, gbc_possitionField);
		possitionField.setColumns(10);
		
		JLabel lblTimeOfWork = new JLabel("Od kiedy pracuje:");
		GridBagConstraints gbc_lblTimeOfWork = new GridBagConstraints();
		gbc_lblTimeOfWork.anchor = GridBagConstraints.EAST;
		gbc_lblTimeOfWork.insets = new Insets(0, 0, 5, 5);
		gbc_lblTimeOfWork.gridx = 1;
		gbc_lblTimeOfWork.gridy = 4;
		getContentPane().add(lblTimeOfWork, gbc_lblTimeOfWork);
		
		timeOfWorkField = new JTextField();
		timeOfWorkField.setEditable(false);
		GridBagConstraints gbc_timeOfWorkField = new GridBagConstraints();
		gbc_timeOfWorkField.insets = new Insets(0, 0, 5, 0);
		gbc_timeOfWorkField.fill = GridBagConstraints.HORIZONTAL;
		gbc_timeOfWorkField.gridx = 2;
		gbc_timeOfWorkField.gridy = 4;
		getContentPane().add(timeOfWorkField, gbc_timeOfWorkField);
		timeOfWorkField.setColumns(10);
		
		btnOkButton = new JButton("Ok");
		GridBagConstraints gbc_btnOkButton = new GridBagConstraints();
		gbc_btnOkButton.gridwidth = 2;
		gbc_btnOkButton.insets = new Insets(0, 0, 0, 5);
		gbc_btnOkButton.gridx = 2;
		gbc_btnOkButton.gridy = 6;
		btnOkButton.addActionListener(this);
		getContentPane().add(btnOkButton, gbc_btnOkButton);
		
		btnEdit = new JButton("Edytuj");
		GridBagConstraints gbc_btnEdit = new GridBagConstraints();
		gbc_btnEdit.gridx = 1;
		gbc_btnEdit.gridy = 6;
		getContentPane().add(btnEdit, gbc_btnEdit);
		btnEdit.addActionListener(this);
		
		this.worker=worker;
		
		nameField.setText(this.worker.getName());
		surnameField.setText(this.worker.getSurname());
		possitionField.setText(this.worker.getPosition());
		
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy/MM/dd");
		
		timeOfWorkField.setText(dateFormat.format(this.worker.getFirstDayOfWork()));
		
		this.workerList = listModel;
		this.workers = workerVector;

	}


	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==btnOkButton)
		{
			if(btnOkButton.getText().equalsIgnoreCase("ok"))this.dispose();
			else{
				
				btnEdit.setText("Edytuj");
				btnOkButton.setText("Ok");
				
				surnameField.setEditable(false);
			}
		}
		else if(e.getSource()==btnEdit){
			
			if(btnEdit.getText()=="Edytuj"){
				
				btnEdit.setText("Zapisz");
				btnOkButton.setText("Anuluj");
				
				surnameField.setEditable(true);
				
			}
			else{
				
				ServerConnector connector = new ServerConnector();
				
				String surname = surnameField.getText();
				
				worker.setSurname(surname);
				
				boolean save=false;
				
				for (Worker tempWorker : workers) {
					
					if(tempWorker.getId()==worker.getId()){
						
						if(!surname.equalsIgnoreCase("")&&connector.UpdateProfile(worker)){
							tempWorker.setSurname(surname);
							save =true;
						}
					}
					
				}
				
				for(int i=0; i<workerList.getSize();i++)
				{
					String[] args = workerList.get(i).toString().split(" ");
					
					if(worker.getId()==Integer.parseInt(args[0])){
						
						if(save){
							
							workerList.remove(i);
							workerList.add(i, worker.getId()+" "+worker.getName()+" "+surname+" "+worker.getPosition());
						}
						}
				}
				
				if(save)this.dispose();
				else JOptionPane.showMessageDialog(rootPane, "Zmiana została odrzucona");
			}
		}
		
	}
}
